package com.szl.group.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.core.MethodParameter;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class ApiUtil {


    /**
     * 按参数名升续拼接参数
     *
     * @param request
     * @return
     */
    public static String concatSignString(HttpServletRequest request) {

        Map<String, String> paramterMap = new HashMap<>();
        request.getParameterMap().forEach((key, value) -> paramterMap.put(key, value[0]));
        // 按照key升续排序，然后拼接参数
        Set<String> keySet = paramterMap.keySet();
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();
        for (String k : keyArray) {
            // 或略掉的字段
            if (k.equals("sign")) {
                continue;
            }
            if (paramterMap.get(k).trim().length() > 0) {
                // 参数值为空，则不参与签名
                sb.append(k).append("=").append(paramterMap.get(k).trim()).append("&");
            }
        }

        return sb.toString();
    }


    /**
     * 按参数名首字母升续拼接参数
     *
     * @param request
     * @return
     */
    public static String concatSignString(HttpServletRequest request,Map<String, String> map,String appSerct) throws IOException {

        String body = IOUtils.toString(request.getInputStream(), StandardCharsets.UTF_8);


        StringBuilder sb = new StringBuilder();
        map.forEach((x,y)-> sb.append(x).append(y));
        sb.append(appSerct).append(body);

        return DigestUtils.md5Hex(sb.toString());
    }

    //
    public static String concatSignString(Map<String, String> map) {

        Map<String, String> paramterMap = new HashMap<>();
        map.forEach((key, value) -> paramterMap.put(key, value));
        // 按照key升续排序，然后拼接参数
        Set<String> keySet = paramterMap.keySet();
        String[] keyArray = keySet.toArray(new String[keySet.size()]);
        Arrays.sort(keyArray);
        StringBuilder sb = new StringBuilder();

        for (String k : keyArray) {
            if (paramterMap.get(k).trim().length() > 0) {
                // 参数值为空，则不参与签名
                sb.append(k).append("=").append(paramterMap.get(k).trim()).append("&");
            }
        }
        return sb.toString();
    }

    /**
     * 获取方法上的@NotRepeatSubmit注解
     *
     * @param handler
     * @return
     */
    public static <T extends Annotation> T getAnnotation(Object handler, Class<T> t) {

        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            MethodParameter[] methodParameters = handlerMethod.getMethodParameters();
            for (int i = 0; i <methodParameters.length ; i++) {
                MethodParameter a=methodParameters[i];
            }
            Method method = handlerMethod.getMethod();
            Parameter[] parameters = method.getParameters();
            for (int i = 0; i <parameters.length ; i++) {
                Parameter parma=parameters[i];

            }
            return method.getAnnotation(t);
        }
        return null;

    }
}
